package bll

import (
	"context"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type answerRecord struct {
	iAnswerRecord store.IAnswerRecord
}

var AnswerRecord = &answerRecord{
	iAnswerRecord: mysql.AnswerRecord,
}

func init() {
	Register(AnswerRecord)
}

func (a *answerRecord) init() func() {
	return func() {}
}

// Create
func (a *answerRecord) Create(ctx context.Context, in *model.AnswerRecordCreateRequest) error {
	var (
		err error
	)
	c := buildAnswerRecord(in)
	_, err = a.iAnswerRecord.Create(ctx, c)
	return err
}

// Update
func (a *answerRecord) Update(ctx context.Context, in *model.AnswerRecordUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.UserId != nil {
		dict["user_id"] = in.UserId
	}

	if in.Difficulty != nil {
		dict["difficulty"] = in.Difficulty
	}

	if in.FirstLevel != nil {
		dict["first_level"] = in.FirstLevel
	}

	if in.SecondLevel != nil {
		dict["second_level"] = in.SecondLevel
	}

	if in.Desc != nil {
		dict["desc"] = in.Desc
	}

	if in.AnswerValue != nil {
		dict["answer_value"] = in.AnswerValue
	}

	if in.CorrectNum != nil {
		dict["correct_num"] = in.CorrectNum
	}

	if in.Total != nil {
		dict["total"] = in.Total
	}

	if in.Star != nil {
		dict["star"] = in.Star
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iAnswerRecord.Update(ctx, in.Id, dict)
}

// Delete
func (a *answerRecord) Delete(ctx context.Context, in *model.AnswerRecordDeleteRequest) error {
	return a.iAnswerRecord.Delete(ctx, in.Id)
}

// List
func (a *answerRecord) List(ctx context.Context, in *model.AnswerRecordListRequest) (*model.AnswerRecordListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.AnswerRecord
		out   = &model.AnswerRecordListResponse{}
	)

	if total, list, err = a.iAnswerRecord.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.AnswerRecordsEntityToDto(list)

	return out, nil
}

// Find
func (a *answerRecord) Find(ctx context.Context, in *model.AnswerRecordInfoRequest) (*model.AnswerRecordInfo, error) {
	var (
		err  error
		data *entity.AnswerRecord
		out  = &model.AnswerRecordInfo{}
	)

	if data, err = a.iAnswerRecord.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.AnswerRecordEntityToDto(data)
	return out, nil
}

// buildAnswerRecord build entity
func buildAnswerRecord(in *model.AnswerRecordCreateRequest) *entity.AnswerRecord {

	ety := &entity.AnswerRecord{}

	ety.UserId = in.UserId

	ety.Difficulty = in.Difficulty

	ety.FirstLevel = in.FirstLevel

	ety.SecondLevel = in.SecondLevel

	ety.Desc = in.Desc

	ety.AnswerValue = in.AnswerValue

	ety.CorrectNum = in.CorrectNum

	ety.Total = in.Total

	ety.Star = in.Star

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
